package com.atguigu.seckill.scheduled;

import com.atguigu.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author kabuda
 * @create 2023-01-11 19:59
 */
@Slf4j
@Component
public class SeckillSkuSchedule {
    @Autowired
    private SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    private final String upload_lock = "seckill:upload:lock";

    //每天凌晨三点上架
    //@Scheduled(cron = "0 0 3 * * ?")
    @Scheduled(cron = "0 */1 * * * ? ")
    public void uploadSeckillSkuLatest3Days() {
        RLock lock = redissonClient.getLock(upload_lock);
        //十秒自动释放锁
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("上架成功");
            seckillService.uploadSeckillSkuLatest3Days();
        } finally {
            lock.unlock();
        }


    }
}
